public class gonggongjiedian {
    /**
     * 找公共节点
     */
     public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
        }
      }
      public int getLength(ListNode head) {
         int length = 0;
         while(head.next != null) {
             head = head.next;
             length++;
         }
         return length;
      }
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null && headB == null) {
            return null;
        }
        int lengthA = getLength(headA);
        int lengthB = getLength(headB);
        int step = Math.abs(lengthA - lengthB);
        if(lengthA > lengthB) {
            while (step > 0) {
                headA = headA.next;
                step--;
            }
        }else {
            while (step > 0) {
                headB = headB.next;
                step--;
            }
        }
        while(headA != null && headB != null) {
            if (headA == headB) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
}
